﻿// Copyright (c) 2014 Morten Bakkedal
// This code is published under the MIT License.

using System;

using FuncLib;
using FuncLib.Functions;
using FuncLib.Mathematics;

namespace FuncLibSamples.Testing.ComplexFunctions
{
	[Serializable]
	public class ComplexRectangularFunction : ComplexFunction
	{
		private Function re, im;

		public ComplexRectangularFunction(Function re, Function im)
		{
			this.re = re;
			this.im = im;
		}

		protected override Complex ComputeValue(IEvaluator evaluator)
		{
			return new Complex(re.Value(evaluator), im.Value(evaluator));
		}

		protected override ComplexFunction ComputeDerivative(ComplexVariable variable)
		{
			// Cauchy-Riemann here??? how about new ComplexRectangularFunction(z.Re(), z.Im()).Derivative(z) where z = new ComplexVariable()??
			return new ComplexRectangularFunction(0.5 * re.Derivative(variable.InnerReVariable), -0.5 * re.Derivative(variable.InnerImVariable));
			//throw new NotImplementedException();
		}

		protected override Function Re()
		{
			return re;
		}

		protected override Function Im()
		{
			return im;
		}
	}
}
